<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/12
 * Time: 10:47
 */

//------------------------
//考勤
//-------------------------

namespace app\attendance\controller;
use app\attendance\Base;
use com\gdl\kq\dataobject\adminstatistics\AdminStatisticsDTO;
use com\gdl\kq\dataobject\platformsetting\platformsettingDTO;
use com\gdl\kq\interfaces\AttAdminStatisticsServiceClient;
use com\gdl\kq\interfaces\PlatformSettingServiceClient;
use think\Config;
use think\Db;
use think\Exception;
use think\Loader;

class AttAdminStatisticsAttendance extends Base
{
    //统计用户考勤详细信息接口
    public function statisticsAdminUserParticularInfos(){
        $unitId = $this->request->param('unitId') ? $this->request->param('unitId') : 0;//部门id  现在是用户传 改完后直接查询管理员部门完成
        $unitIds = $this->request->param('unitIds') ? $this->request->param('unitIds') : 0;//部门ids
        $date = $this->request->param('date') ? $this->request->param('date') : 0;//2018-07-01 2018-07-31
        $offset = $this->request->param('offset') ? $this->request->param('offset/d') : 1;
        $num = $this->request->param('num') ? $this->request->param('num/d') : 10;
        $memberIds = $this->request->param('memberIds') ? $this->request->param('memberIds') : '0';
        $adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
        $adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
        $agId = $this->request->param('agId') ? getStr($this->request->param('agId')) : 0;
        $platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : 0;
        if(!$platformId || $adminReg==''){
            $msg =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
            return json_encode($msg);
        }
        try{
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
            $api = new AttAdminStatisticsServiceClient(NULL);
            \ApiClient::build($api);
            $AdminStatisticsDTO = new AdminStatisticsDTO();
            $data = [
                'offset'=>$offset,
                'date'=>$date,
                'agId'=>$agId,
                'num'=>$num,
                'adminReg'=>$adminReg,
                'unitId'=>$unitId,
                'unitIds'=>$unitIds,
                'memberIds'=>$memberIds,
                'adminUserId'=>$adminUserId,
            ];
            $AdminStatisticsDTO->data = $data;
            $AdminStatisticsDTO->platformId = $platformId;
            #调用接口
            $result = $api->statisticsAdminUserParticularInfos($AdminStatisticsDTO);

            if($result->code){
                $pro = [];
                foreach (json_decode($result->data[0][0]) as $val){
                    $pro[] =  $val;
                }
                $msg =  ['msg'=>'获取成功','code'=>'200','data'=>$pro,'count'=>$result->counts];
            } else {
                $msg =  ['msg'=>'获取失败','code'=>'400','data'=>[],'count'=>0];
            }
        } catch (Exception $ex) {
            $msg =  ['msg'=>'异常','code'=>'500','data'=>[],'count'=>0];
        }

        return json_encode($msg);
    }

    //通过日期获取人员打卡详细
    public function getUserClockinRecordByDate(){
        $date = $this->request->param('date') ? $this->request->param('date') : date('Y-m-d',time());//日期 2018-07-16 如果为空则为今天
        $userId = $this->request->param('userId') ? getStr($this->request->param('userId')) : 0;
        if(!$userId){
            $msg =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
            return json_encode($msg);
        }
        try{
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
            $api = new AttAdminStatisticsServiceClient(NULL);
            \ApiClient::build($api);
            $AttendanceGroupDTO = new AdminStatisticsDTO();
            $data = [
                'date'=>$date,
                'userId'=>$userId,
            ];
            $AttendanceGroupDTO->data = $data;
            #调用接口
            $result = $api->getAdminUserClockinRecordByDate($AttendanceGroupDTO);
            if($result->code){
                $msg =  ['msg'=>'获取成功','code'=>'200','data'=>json_decode($result->data[0][0])];
            } else {
                $msg =  ['msg'=>'获取失败','code'=>'400','data'=>[]];
            }
        } catch (Exception $ex) {
            $msg =  ['msg'=>'异常','code'=>'500','data'=>[]];
        }

        return json_encode($msg);
    }

    //平台统计接口
    public function statisticsPlatformClockinGet(){
        $unitId = $this->request->param('unitId') ? $this->request->param('unitId') : 0;//部门id  现在是用户传 改完后直接查询管理员部门完成
        $unitIds = $this->request->param('unitIds') ? $this->request->param('unitIds') : 0;//部门ids
        $date = $this->request->param('date') ? $this->request->param('date') : 0;//2018-07-01 2018-07-31
        $offset = $this->request->param('offset') ? $this->request->param('offset/d') : 1;
        $num = $this->request->param('num') ? $this->request->param('num/d') : 10;
        $memberIds = $this->request->param('memberIds') ? $this->request->param('memberIds') : '0';
        $adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
        $adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
        $platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : 0;
        $agId = $this->request->param('agId') ? getStr($this->request->param('agId')) : 0;//考勤ID
        $agId = $this->request->param('agId') ? getStr($this->request->param('agId')) : 0;//考勤ID
        if(!$platformId || $adminReg==''){
            $msg =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
            return json_encode($msg);
        }
        try{
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
            $api = new AttAdminStatisticsServiceClient(NULL);
            \ApiClient::build($api);
            $AdminStatisticsDTO = new AdminStatisticsDTO();
            $data = [
                'offset'=>$offset,
                'date'=>$date,
                'num'=>$num,
                'adminReg'=>$adminReg,
                'unitId'=>$unitId,
                'unitIds'=>$unitIds,
                'memberIds'=>$memberIds,
                'adminUserId'=>$adminUserId,
                'agId'=>$agId,
            ];
            $AdminStatisticsDTO->data = $data;
            $AdminStatisticsDTO->platformId = $platformId;
            #调用接口
            $result = $api->getStatisticsPlatformClockin($AdminStatisticsDTO);
            if($result->code){
                $pro = [];
                foreach (json_decode($result->data[0][0]) as $val){
                    $pro[] =  $val;
                }
                $msg =  ['msg'=>'获取成功','code'=>'200','data'=>$pro,'count'=>$result->counts];
            } else {
                $msg =  ['msg'=>'获取失败','code'=>'400','data'=>[],'count'=>0];
            }
        } catch (Exception $ex) {
            $msg =  ['msg'=>'异常','code'=>'500','data'=>[],'count'=>0];
        }

        return json_encode($msg);
    }

    // 下载统计execl
    public function downExeclAdminUserParticularInfo(){
        ob_end_clean(); # 清空缓冲区
        $msg = ['msg'=>'没有可导出的数据','code'=>'400','data'=>[]];
        # 公共参数
        $adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
        $adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
        $platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : ""; #平台id
        if(empty($platformId)){ $msg['msg'] = "请选择平台"; return json_encode($msg);}

        #其他参数
        $unitId = $this->request->param('unitId') ? $this->request->param('unitId') : 0;//部门id  现在是用户传 改完后直接查询管理员部门完成
        $unitIds = $this->request->param('unitIds') ? $this->request->param('unitIds') : 0;//部门ids
        $date = $this->request->param('date') ? $this->request->param('date') : 0;//2018-07-01 2018-07-31
        $offset = $this->request->param('offset') ? $this->request->param('offset/d') : 1;
        $num = $this->request->param('num') ? $this->request->param('num/d') : 10;
        $memberIds = $this->request->param('memberIds') ? $this->request->param('memberIds') : '0';
        if(!$platformId || $adminReg==''){
            $msg =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
            return json_encode($msg);
        }


        #其他参数
        try {
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
            $api = new AttAdminStatisticsServiceClient(NULL);
            \ApiClient::build($api);
            $AdminStatisticsDTO = new AdminStatisticsDTO();
            $data = [
                'offset'=>$offset,
                'date'=>$date,
                'num'=>$num,
                'adminReg'=>$adminReg,
                'unitId'=>$unitId,
                'unitIds'=>$unitIds,
                'memberIds'=>$memberIds,
                'adminUserId'=>$adminUserId,
            ];
            $AdminStatisticsDTO->data = $data;
            $AdminStatisticsDTO->platformId = $platformId;
            #调用接口
            $result = $api->statisticsAdminUserParticularInfos($AdminStatisticsDTO);
            if($result->success == true){
                $pro = [];
                $data = json_decode($result->data[0][0]);
                foreach ($data as &$val){
                    unset($val->id);
                    $pro[] =  $val;
                }
                $pro = object_to_array($pro);
//                print_r($pro);die;
                $header = [
                    '姓名','职位'
                    ,'所在单位','上级单位'
                    ,'所在考勤组','日期/周'
                ];

                \Excel::exportOrderExcel2( $header,$pro, date('Y-m-d H:i:s',time())."用户考勤详细信息统计");
                exit;
            }

        } catch (Exception $ex) {
            $msg['msg'] = $ex->getMessage();
        }
        return json_encode($msg);
    }

    // 平台人员考勤统计execl
    public function downExeclAdminUserClockinInfo(){
        ob_end_clean(); # 清空缓区
        $msg = ['msg'=>'没有可导出的数据','code'=>'400','data'=>[]];
        # 公共参数
        $adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
        $adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
        $platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : ""; #平台id
        if(empty($platformId)){ $msg['msg'] = "请选择平台"; return json_encode($msg);}

        #其他参数
        $unitId = $this->request->param('unitId') ? $this->request->param('unitId') : 0;//部门id  现在是用户传 改完后直接查询管理员部门完成
        $unitIds = $this->request->param('unitIds') ? $this->request->param('unitIds') : 0;//部门ids
        $date = $this->request->param('date') ? $this->request->param('date') : 0;//2018-07-01 2018-07-31
        $offset = $this->request->param('offset') ? $this->request->param('offset/d') : 1;
        $num = $this->request->param('num') ? $this->request->param('num/d') : 10;
        $memberIds = $this->request->param('memberIds') ? $this->request->param('memberIds') : '0';
        $agId = $this->request->param('agId') ? getStr($this->request->param('agId')) : 0;//考勤ID
        if(!$platformId || $adminReg==''){
            $msg =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
            return json_encode($msg);
        }
        try{
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
            $api = new AttAdminStatisticsServiceClient(NULL);
            \ApiClient::build($api);
            $AdminStatisticsDTO = new AdminStatisticsDTO();
            $data = [
                'offset'=>$offset,
                'date'=>$date,
                'num'=>$num,
                'adminReg'=>$adminReg,
                'unitId'=>$unitId,
                'unitIds'=>$unitIds,
                'memberIds'=>$memberIds,
                'adminUserId'=>$adminUserId,
                'agId'=>$agId,
            ];
            $AdminStatisticsDTO->data = $data;
            $AdminStatisticsDTO->platformId = $platformId;
            #调用接口
            $result = $api->getStatisticsPlatformClockin($AdminStatisticsDTO);

            if($result->success == true){
                $pro = [];
                $data = json_decode($result->data[0][0]);

                foreach ($data as &$val){
                    unset($val->id);
                    $pro[] =  $val;
                }
                $pro = object_to_array($pro);

                $header = [
                    '姓名','职位'
                    ,'所在单位','上级单位'
                    ,'所在考勤组','工作日天数'
                    ,'考勤正常天数','迟到次数'
                    ,'早退次数','累积旷工时间'
                    ,'上班缺卡次数','下班缺卡次数'
                    ,'外出天数',[["请假天数"],["年假","事假","产假","婚假","丧假","护理假"]]];

                \Excel::exportOrderExcel( $header,$pro, date('Y-m-d H:i:s',time())."平台人员考勤统计");
                exit;
            }

        } catch (Exception $ex) {
            $msg['msg'] = $ex->getMessage();
        }
        return json_encode($msg);
    }

    //首页人脸条数统计
    public function statisticsPlatformFaceNum(){
        $adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
        $adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
        $platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : 0;
        $id = $this->request->param('id') ? getStr($this->request->param('id')) : 0;

        if(!$platformId || $adminReg==''){
            $msg =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
            return json_encode($msg);
        }
        try{
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $api = new PlatformSettingServiceClient(NULL);
            \ApiClient::build($api);
            $platformsettingDTO = new platformsettingDTO();
            $platformsettingDTO->id	= $id;
            $platformsettingDTO->key	= "faceNum";
            $platformsettingDTO->platformId	= $platformId;
            $platformsettingDTO->data	= [
                'adminUserId'=>$adminUserId,
                'adminReg'=>$adminReg
            ];

            $result = $api->getPlatformSetting($platformsettingDTO);
            $pdata = [
                'totalNum'=>0,
                'usageRate'=>0,
                'useFaceNum'=>0,
                'surplusFaceNum'=>0,
            ];
            if($result->code){
                $num = $result->data[0]['value'];
                $pdata['totalNum'] = $num?$num:0;
                $AttAdminStatisticsService = new AttAdminStatisticsServiceClient(NULL);
                \ApiClient::build($AttAdminStatisticsService);
                $AdminStatisticsDTO = new AdminStatisticsDTO();
                $AdminStatisticsDTO->platformId;
                $FaceInfo = $AttAdminStatisticsService->getFaceInfoNum($AdminStatisticsDTO);
                $useFaceNum = 0 ;
                if ($FaceInfo)$useFaceNum = $FaceInfo->data[0][0]?$FaceInfo->data[0][0]:0;
                $pdata['totalNum'] = $useFaceNum;
                $pdata['usageRate'] = round($useFaceNum/$pdata['totalNum']*100,2)."%";
                $pdata['surplusFaceNum'] = $num>$useFaceNum?($num-$useFaceNum):0;
            }
            $msg =  ['msg'=>"成功",'code'=>200,'data'=>$pdata,'count'=>0];
        } catch (Exception $ex) {
            $msg =  ['msg'=>'异常','code'=>'500','data'=>[],'count'=>0];
        }

        return json_encode($msg);
    }

    //首页概况统计
    public function statisticsPlatformGeneral(){
        $adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
        $adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
        $platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : 0;
        $id = $this->request->param('id') ? getStr($this->request->param('id')) : 0;

        if(!$platformId || $adminReg==''){
            $msg =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
            return json_encode($msg);
        }
        try{
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $api = new AttAdminStatisticsServiceClient(NULL);
            \ApiClient::build($api);
            $AdminStatisticsDTO = new AdminStatisticsDTO();
            $AdminStatisticsDTO->id	= $id;
            $AdminStatisticsDTO->platformId	= $platformId;
            $AdminStatisticsDTO->data	= [
                'adminUserId'=>$adminUserId,
                'adminReg'=>$adminReg
            ];

            $result = $api->getStatisticsPlatformGeneral($AdminStatisticsDTO);
            $pdata = [
                'CompanyUserNum'=>0,
                'groupNum'=>0,
                'unitNum'=>0
            ];
            if($result->code){
                $pdata = $result->data[0];
            }
            $msg =  ['msg'=>"成功",'code'=>200,'data'=>$pdata,'count'=>0];
        } catch (Exception $ex) {
            $msg =  ['msg'=>'异常','code'=>'500','data'=>[],'count'=>0];
        }

        return json_encode($msg);
    }

    //首页考勤异常统计
    /**
     * @return string
     */
    public function statisticsUnusual(){
        $adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
        $adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
        $platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : 0;
        $id = $this->request->param('id') ? getStr($this->request->param('id')) : 0;
        //seven=>7天  week=>1周 month=>1个月 day=>自定义时间
        $days = $this->request->param('days') ? getStr($this->request->param('days')) : 0;//
        $typesof = $this->request->param('typesof') ? getStr($this->request->param('typesof')) : 'time';//
        $datime = $this->request->param('datime') ? getStr($this->request->param('datime')) : '';//

        if(!$platformId || $adminReg==''){
            $msg =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
            return json_encode($msg);
        }
        try{
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $api = new AttAdminStatisticsServiceClient(NULL);
            \ApiClient::build($api);
            $AdminStatisticsDTO = new AdminStatisticsDTO();
            $AdminStatisticsDTO->id	= $id;
            $AdminStatisticsDTO->platformId	= $platformId;
            $AdminStatisticsDTO->data	= [
                'days'=>$days,
                'typesof'=>$typesof,
                'datime'=>$datime,
                'adminUserId'=>$adminUserId,
                'adminReg'=>$adminReg
            ];

            $result = $api->getStatisticsUnusual($AdminStatisticsDTO);
            $data = $result->data;
            if(!empty($data[0])){
                $sums = array_sum($data[0]);
                if($sums){
                    foreach($data[0] as $v){

                        $percentage = ($v*100)/$sums;
                        $data[2][] = round($percentage, 2);
                    }
                } else {
                    foreach($data[0] as $v){
                        $data[2][] = 0;
                    }

                }

                if($typesof=='month' && substr_count($data[1][0],'-')>1){
                    for($i=0;$i<count($data[1]);$i++){
                        $data[1][$i] = substr($data[1][$i],5);
                    }
                }

            } else {
                $data = [[],[],[]];
            }
            $msg =  ['msg'=>"成功",'code'=>200,'data'=>$data,'count'=>0];
        } catch (Exception $ex) {
            $msg =  ['msg'=>'异常','code'=>'500','data'=>[],'count'=>0];
        }

        return json_encode($msg);
    }

    //考勤请假统计
    /**
     * @return string
     */
    public function getStatisticsLeave(){
        $type = $this->request->param('type') ? $this->request->param('type') : 1;//1请假统计 2人员统计
        $adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
        $unitId = $this->request->param('unitId') ? $this->request->param('unitId') : 0;
        $adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
        $platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : 0;
        $id = $this->request->param('id') ? getStr($this->request->param('id')) : 0;
        if(!$platformId || $adminReg==''){
            $msg =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
            return json_encode($msg);
        }
        try{
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $api = new AttAdminStatisticsServiceClient(NULL);
            \ApiClient::build($api);
            if ($type==1){
                $data1= ["年假", "事假", "病假", "婚假", "产假"];
                $data2= [
                    ['value'=>0,'name'=>"年假"],
                    ['value'=>0,'name'=>"事假"],
                    ['value'=>0,'name'=>"病假"],
                    ['value'=>0,'name'=>"婚假"],
                    ['value'=>0,'name'=>"产假"],
                ];
                $AdminStatisticsDTO = new AdminStatisticsDTO();
                $AdminStatisticsDTO->id	= $id;
                $AdminStatisticsDTO->platformId	= $platformId;
                $AdminStatisticsDTO->data	= [
                    'adminUserId'=>$adminUserId,
                    'adminReg'=>$adminReg,
                    'unitId'=>$unitId,
                    'data2'=>serialize($data2)
                ];
                $result = $api->getStatisticsLeave($AdminStatisticsDTO);
                $data = $result->data?$result->data:$data2;
                $msg =  ['msg'=>"成功",'code'=>200,'data'=>[$data1,$data],'count'=>0];
            }else{
                $AdminStatisticsDTO = new AdminStatisticsDTO();
                $AdminStatisticsDTO->id	= $id;
                $AdminStatisticsDTO->platformId	= $platformId;
                $AdminStatisticsDTO->data	= [
                    'type'=>$type,
                    'adminUserId'=>$adminUserId,
                    'adminReg'=>$adminReg,
                    'unitId'=>$unitId
                ];
                $result = $api->getStatisticsLeave($AdminStatisticsDTO);
                $data = $result->data?$result->data:[[],[]];
                $msg =  ['msg'=>"成功",'code'=>200,'data'=>$data,'count'=>0];
            }

        } catch (Exception $ex) {
            $msg =  ['msg'=>'异常','code'=>'500','data'=>[[],[]],'count'=>0];
        }

        return json_encode($msg);
    }

    //最新实时考勤
    /**
     * @return string
     */
    public function getStatisticsNewClockin(){
        $adminUserId = $this->request->param('adminUserId') ? $this->request->param('adminUserId') : 0;
        $adminReg = $this->request->param('adminReg') ? getStr($this->request->param('adminReg')) : '';
        $platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : 0;
        $id = $this->request->param('id') ? getStr($this->request->param('id')) : 0;
        $unitId = $this->request->param('unitId') ? getStr($this->request->param('unitId')) : 0;

        if(!$platformId || $adminReg==''){
            $msg =  ['msg'=>'缺少参数','code'=>'500','data'=>[]];
            return json_encode($msg);
        }
        try{
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $api = new AttAdminStatisticsServiceClient(NULL);
            \ApiClient::build($api);
            $AdminStatisticsDTO = new AdminStatisticsDTO();
            $AdminStatisticsDTO->id	= $id;
            $AdminStatisticsDTO->platformId	= $platformId;
            $AdminStatisticsDTO->data	= [
                'adminUserId'=>$adminUserId,
                'adminReg'=>$adminReg,
                'unitId'=>$unitId
            ];
            $result = $api->getStatisticsNewClockin($AdminStatisticsDTO);
            $data = $result->data?$result->data:[];
            $msg =  ['msg'=>"成功",'code'=>200,'data'=>$data,'count'=>0];
        } catch (Exception $ex) {
            $msg =  ['msg'=>'异常','code'=>'500','data'=>[],'count'=>0];
        }

        return json_encode($msg);
    }
}